<template>
  <div class="cart">
    <div class="header">
      购物车
      <span @click="remove">清空</span>
    </div>
    <ul>
      <li v-for="(ca,i) in cart" :key="i">
        <p>{{ca.name}}</p>
        <div class="money">
          <span>￥</span>
          <span>{{ca.money}}</span>
        </div>
        <span></span>
        <div class="add">
          <span @click="rom(ca.id)">-</span>
          <span>{{ca.num}}</span>
          <span @click="increase(ca.id)">+</span>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      cart: []
    }
  },
  methods: {
    newData() {
      this.cart = []
      let shopRecord = JSON.parse(localStorage.getItem(this.$route.query.id))
      for (var key in shopRecord) {
        //把店铺中每个商品购物车数据都添加到数组中
        this.cart.push({
          id: key,
          name: shopRecord[key]['name'],
          money: shopRecord[key]['money'],
          num: shopRecord[key]['num']
        })
      }
    },
    increase(id) {
      let shopRecord = JSON.parse(localStorage.getItem(this.$route.query.id))
      shopRecord[id]['money'] += shopRecord[id]['money'] / shopRecord[id]['num']
      shopRecord[id]['num'] += 1
      this.$emit('addNum', id, shopRecord[id]['num'], shopRecord[id]['money'])
      this.newData()
    },
    rom(id) {
      let shopRecord = JSON.parse(localStorage.getItem(this.$route.query.id))
      shopRecord[id]['money'] -= shopRecord[id]['money'] / shopRecord[id]['num']
      shopRecord[id]['num'] -= 1
      this.$emit('romNum', id, shopRecord[id]['num'], shopRecord[id]['money'])
      this.newData()
    },
    remove() {
      let shopRecord = JSON.parse(localStorage.getItem(this.$route.query.title))
      for (var dish in shopRecord) {
        this.$emit('romNum', dish, 0, 0)
      }
    }
  },
  created() {
    this.newData()
  }
}
</script>
<style  scoped>
/*@keyframes cart_show{
    0%{transform: translateY(100%); }
    100%{transform: translateY(0%); }
}*/
.cart {
  position: fixed;
  bottom: 40px;
  background-color: #aaa;
  width: 100%;
  /*animation: cart_show 1s ; */
  z-index: 999;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.3rem 0.6rem;
  background-color: #eceff1;
}
ul {
  padding: 0;
  margin: 0;
  border: none;
  font-weight: normal;
}
li {
  margin: 0;
  font-size: 0.7rem;
  color: #666;
  font-weight: bold;
  padding: 0.6rem 0.5rem;
  background-color: white;
  justify-content: space-between;
  display: flex;
}
.money > span {
  font-size: 0.8rem;
  color: #ff5f3e;
}
p {
  margin: 0;
  flex: 1;
}
.add {
  display: inline-flex;
  margin-left: 1rem;
}
.add > span:last-child,
.add > span:first-child {
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 50%;
  border: none;
  text-align: center;
  font-weight: bold;
  background-color: #26a2ff;
  font-size: 0.7rem;
}
.add > span:nth-child(2) {
  margin: 0 0.5rem;
  font-size: 0.8rem;
  font-weight: bold;
}
.add > div:last-child {
  margin-right: 0;
  margin-left: 0.5rem;
}
</style>
